package org.jeecg.modules.clue.controller;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.query.QueryRuleEnum;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.clue.entity.ClueTaskPushDayReport;
import org.jeecg.modules.clue.service.IClueTaskPushDayReportService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 线索任务推送日报表
 * @Author: jeecg-boot
 * @Date:   2025-01-15
 * @Version: V1.0
 */
@Api(tags="线索任务推送日报表")
@RestController
@RequestMapping("/clue/clueTaskPushDayReport")
@Slf4j
public class ClueTaskPushDayReportController extends JeecgController<ClueTaskPushDayReport, IClueTaskPushDayReportService> {
	@Autowired
	private IClueTaskPushDayReportService clueTaskPushDayReportService;


	/**
	 * 分页列表查询
	 *
	 * @param clueTaskPushDayReport
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "线索任务推送日报表-分页列表查询")
	@ApiOperation(value="线索任务推送日报表-分页列表查询", notes="线索任务推送日报表-分页列表查询")
	@GetMapping(value = "/list")
	@PermissionData(pageComponent="clue/pushDayReport/ClueTaskPushDayReportList")
	public Result<IPage<ClueTaskPushDayReport>> queryPageList(ClueTaskPushDayReport clueTaskPushDayReport,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String roleCode = sysUser.getRoleCode();
		List<String> roleCodes = Arrays.asList(Optional.ofNullable(roleCode).orElse("").split(","));

		String yesterdayDateStr = DateUtil.format(DateUtil.yesterday().toJdkDate(), "yyyy-MM-dd");

		QueryWrapper<ClueTaskPushDayReport> queryWrapper = QueryGenerator.initQueryWrapper(clueTaskPushDayReport, req.getParameterMap());
		Page<ClueTaskPushDayReport> page = new Page<ClueTaskPushDayReport>(pageNo, pageSize);
		IPage<ClueTaskPushDayReport> pageList = clueTaskPushDayReportService.page(page, queryWrapper);
		List<ClueTaskPushDayReport> collect = pageList.getRecords().stream().map(new Function<ClueTaskPushDayReport, ClueTaskPushDayReport>() {
			@Override
			public ClueTaskPushDayReport apply(ClueTaskPushDayReport clueTaskPushDayReport) {
				ClueTaskPushDayReport newClueTaskPushDay = new ClueTaskPushDayReport();
				BeanUtils.copyProperties(clueTaskPushDayReport, newClueTaskPushDay);
				// 当前角色不是 管理员
                if (!roleCodes.contains("plain_admin") &&  !roleCodes.contains("admin")) {
                   // 今天4点之前的建单量不显示
					 Date pushTime = clueTaskPushDayReport.getPushDate();
					String createDateStr = DateUtil.format(pushTime, "yyyy-MM-dd");
                    if (yesterdayDateStr.equals(createDateStr)) {
                        if (DateUtil.hour(DateUtil.date(),true)  < 16) {
							newClueTaskPushDay.setBuildNum(null);
                        }
                    }

				}

				return newClueTaskPushDay;
			}
		}).collect(Collectors.toList());
		pageList.setRecords(collect);
		return Result.OK(pageList);
	}
	/**
	 * 汇总查询
	 *
	 * @return
	 */
	//@AutoLog(value = "线索任务推送日报表-汇总查询")
	@ApiOperation(value="线索任务推送日报表-汇总查询", notes="线索任务推送日报表-汇总查询")
	@GetMapping(value = "/summaryList")
	public Result<List<JSONObject>> summaryList( HttpServletRequest req) {

		List<JSONObject> summaryList = clueTaskPushDayReportService.summaryList(req);
		return Result.OK(summaryList);




	}




	/**
	 *   添加
	 *
	 * @param clueTaskPushDayReport
	 * @return
	 */
	@AutoLog(value = "线索任务推送日报表-添加")
	@ApiOperation(value="线索任务推送日报表-添加", notes="线索任务推送日报表-添加")
	@RequiresPermissions("clue:clue_task_push_day_report:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody ClueTaskPushDayReport clueTaskPushDayReport) {
		clueTaskPushDayReportService.save(clueTaskPushDayReport);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param clueTaskPushDayReport
	 * @return
	 */
	@AutoLog(value = "线索任务推送日报表-编辑")
	@ApiOperation(value="线索任务推送日报表-编辑", notes="线索任务推送日报表-编辑")
	@RequiresPermissions("clue:clue_task_push_day_report:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody ClueTaskPushDayReport clueTaskPushDayReport) {
		clueTaskPushDayReportService.updateById(clueTaskPushDayReport);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "线索任务推送日报表-通过id删除")
	@ApiOperation(value="线索任务推送日报表-通过id删除", notes="线索任务推送日报表-通过id删除")
	@RequiresPermissions("clue:clue_task_push_day_report:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		clueTaskPushDayReportService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "线索任务推送日报表-批量删除")
	@ApiOperation(value="线索任务推送日报表-批量删除", notes="线索任务推送日报表-批量删除")
	@RequiresPermissions("clue:clue_task_push_day_report:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.clueTaskPushDayReportService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "线索任务推送日报表-通过id查询")
	@ApiOperation(value="线索任务推送日报表-通过id查询", notes="线索任务推送日报表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<ClueTaskPushDayReport> queryById(@RequestParam(name="id",required=true) String id) {
		ClueTaskPushDayReport clueTaskPushDayReport = clueTaskPushDayReportService.getById(id);
		if(clueTaskPushDayReport==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(clueTaskPushDayReport);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param clueTaskPushDayReport
    */
    @RequiresPermissions("clue:clue_task_push_day_report:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ClueTaskPushDayReport clueTaskPushDayReport) {
        return super.exportXls(request, clueTaskPushDayReport, ClueTaskPushDayReport.class, "线索任务推送日报表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("clue:clue_task_push_day_report:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ClueTaskPushDayReport.class);
    }


}
